package com.fenghuajueli.lib_statistics;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;


import java.util.Map;

/**
 * ===========================================================
 * author：songpeng on 1/6/21 16:33
 * email：fenghuajueli@gmail.com
 * ===========================================================
 */
public class UmEventUtils {

    private static Application application;

    /**
     * 初始化统计事件工具类
     *
     * @param context
     */
    public static void init(Application context) {
        application = context;
    }


    private static Handler mHandler = new Handler(Looper.getMainLooper());

    /**
     * 判断当前调用该方法所在的线程是否为主线程即UI线程
     *
     * @return
     */
    private static boolean isInMainThread() {
        long mainThreadId = Looper.getMainLooper().getThread().getId();
        long curThreadId = Thread.currentThread().getId();
        return mainThreadId == curThreadId;
    }

    /**
     * 友盟带参数统计事件上报
     *
     * @param eventId
     * @param params
     */
    public static void onEvent(final String eventId, final Map<String, String> params) {
        if (application == null || TextUtils.isEmpty(eventId)) {
            return;
        }
       /* if (isInMainThread()) {
            if (params == null) {
                MobclickAgent.onEvent(application, eventId);
            } else {
                MobclickAgent.onEvent(application, eventId, params);
            }
        } else {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (params == null) {
                        MobclickAgent.onEvent(application, eventId);
                    } else {
                        MobclickAgent.onEvent(application, eventId, params);
                    }
                }
            });
        }*/
    }

    /**
     * 友盟统计事件上报
     *
     * @param eventId
     */
    public static void onEvent(final String eventId) {
        if (application == null || TextUtils.isEmpty(eventId)) {
            return;
        }
        Log.d("UmEventUtils", "事件名称--------->" + eventId);
      /*  if (isInMainThread()) {
            MobclickAgent.onEvent(application, eventId);
        } else {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    MobclickAgent.onEvent(application, eventId);
                }
            });
        }*/
    }


}
